scripts
from byubit import Bit
debugger
return
%
=> is_even)how_many_rows
using //
and %
None
Writing and running python scripts
simple_script.py
¶NOTES
Bit
print
statement) before the next line is executedThen
Then
print_stuff.py
¶NOTES
print_stuff.py
simple_script.py
ran instead!print('Bit...
say_it_twice
on the second statementsay_it_twice
message
print('Bit...
and say_it_with_a_preamble('I love...
return
¶def add(a, b):
result = a + b
return result
number = add(7, 9)
print(number)
NOTES
number = add(7, 9)
Before number
gets a value, python needs to evaluate add(7, 9)
So add
runs with a = 7
and b = 9
.
result = 7 + 9 = 16
Then add
returns the value 16
.
Now, add(7, 9)
is replaced with its returned value 16
, which is assigned to number
.
def add(a, b):
return a + b
number = add(3, 4)
print(number)
NOTES
return
returns a value. You can use a variable to reference the value, or you can some other expression to describe the value.Variables are assigned values.
Functions return values.
An expression is a recipe for getting a value.
When a value is needed but an expression is provided, the expression is evaluated to get a value.
An expression can be:
7
, 'hello'
number
add(7, 8)
number + 7 + add(2, 3)
%%file fancy_math.py
def add_seven(number):
return number + 7
def is_big(number):
return number > 10
def make_smaller(number):
if is_big(number):
return number - 10
else:
return number - 1
def main(number):
number = add_seven(number)
number = make_smaller(number)
print(number)
number = 7
main(number)
fancy_math.py
¶NOTES
Run script
is_big
returns a bool
, while add_seven
and make_smaller
return int
s. Step through with a debugger
number = 7
7
number
number
in main, add_seven
, is_big
, and make_smaller
number
changes as you step over the last lines of the scriptis_big
inside make_smaller
%%file test_fancy_math.py
from fancy_math import add_seven, is_big
def test_add_seven():
assert add_seven(1) == 8
assert add_seven(10) == 17
def test_is_big():
assert is_big(11)
assert not is_big(2)
test_fancy_math.py
¶NOTES
test_fancy_math.py
What does a function return when it doesn't return
anything?
None
¶def print_something():
print('something')
result = print_something()
print(result)
type(result)
result is None
None
is the special value that means nothing.
Every variable must point to something. So, if a variable points to nothing, it points to None
.
Every function must return something, so if it returns nothing, it returns None
.
By default, if you don't specify a return
statement in your function, it returns None
.
None + 1
foo = None
foo / 1
TypeError
referencing NoneType, it probably means you have an unexpected None
popping up.
Use the debugger to figure out where the None
is coming from.
$11 \div 3$ or $\frac{11}{3}$
print(11 / 3)
Or to get just the integer part of the division:
print(11 // 3)
To get the remainder of division:
print(11 % 3)
print(25 % 2)
The //
operator is the integer division operator.
The %
is the modulus operator.
Perhaps when you were learning division in grade school, you started with problems like:
$ 7 \div 3 = 2 r 1 $
Seven divided by three equals two remainder one
The //
operator gives you the integer quotient (the "two").
The %
operator gives you the remainder (the "one").
$2 \times 5$ or $2 \cdot 5$
print(2 * 5)
$2^5$
print(2 ** 5)
print(7 == 8)
print(7 == 7)
print(3 + 4 == 7)
number = 3
print(number + 4 == 7)
number = 3
print((number + 4) == 7)
how_many_rows.py
¶NOTES
return people / row_width
), use the debugger to demonstrate how to backtrack information to the source of the problemrows = people // row_width
if
block under certain conditionsdef how_many_rows(people, row_width):
"""Return the number of rows needed to seat the guests, given a row width."""
rows = people // row_width
if people % row_width != 0:
rows = rows + 1
return rows
def display_information(num_guests, row_width, rows):
max_capacity = rows * row_width
extra = max_capacity - guests
print(f'With {guests} guests and rows of {row_width} seats:')
print(f' You need {rows} rows resulting in {extra} extra seats')
def main(guests, row_width):
rows = how_many_rows(guests, row_width)
display_information(guests, row_width, rows)
# Set guests and row width
guests = 223
row_width = 25
main(guests, row_width)
return
+ - / // % * **
None
is the value that means nothing